var app = getApp();
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    czjl: app.myUlife.ulifeUrl + 'wx/title_script_img.png',//成长记录字
    username: '',//姓名
    avatarUrl: '',//我的头像
    gender: '',
    slide: app.myUlife.ulifeUrl + 'wx/slide_btn_icon.png',//默认图片
    data_bg_img: app.myUlife.ulifeUrl + 'wx/data_bg_img.jpg',//成长记录

    //滑动
    scrollindex: 0,
    totalnum: 2,  //总共页面数
    starty: 0,  //开始的位置x 
    endy: 0, //结束的位置y
    margintop: 0,  //滑动下拉距离

    planlog:0,//我阅读的书籍总数
    plog: 0,//我的诗词完成任务数
    hchjhd: 0,//好词好句号段数
    zjdhg: 0,//章节读后感

    //框
    k1: app.myUlife.ulifeUrl + 'kuang/k1.png',
    k2: app.myUlife.ulifeUrl + 'kuang/k2.png',
    k3: app.myUlife.ulifeUrl + 'kuang/k3.png',
    k4: app.myUlife.ulifeUrl + 'kuang/k4.png',
    k5: app.myUlife.ulifeUrl + 'kuang/k5.png',
    k_bg_img: app.myUlife.ulifeUrl + 'kuang/k_bg_img.jpg',


    showsqModalStatus: false,//提示弹出
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }
    var user_type = wx.getStorageSync('user_type');
    var gender = wx.getStorageSync('gender');
    this.setData({
      gender: gender,
      avatarUrl: wx.getStorageSync('avatarUrl'),
      username: wx.getStorageSync('name'),
      user_type: user_type
    });
    //获取统计
    this.getMyCzStatic();
  },

  //获取我的成长记录
  getMyCzStatic(){
    var that=this;
    var usid = wx.getStorageSync('usid');
    app.wxget({
      url:'staticlog/getczjl.html',
      data: {
        usid: usid
      },
      success: function (res) {
        that.setData({
          planlog: res.data.planlog,//我阅读的书籍总数
          countword: res.data.countword,//我阅读的书籍的字数统计
          plog: res.data.plog,//我的诗词完成任务数 
          hchjhd: res.data.hchjhd,//我的阅读记录
          zjdhg: res.data.zjdhg,//章节读后感
          ydjy: res.data.ydjy,//阅读剪影
        })
      }
    })
  },

  //滑动开始 start
  scrollTouchstart: function (e) {
    let py = e.touches[0].pageY;
    this.setData({
      starty: py
    })
  }, 
  scrollTouchmove: function (e) {
    let py = e.touches[0].pageY;
    let d = this.data;
    this.setData({
      endy: py,
    })
    if (py - d.starty < 100 && py - d.starty > -100) {
      this.setData({
        margintop: py - d.starty
      })
    }
  },
  scrollTouchend: function (e) {
    let d = this.data;
    if (d.endy - d.starty > 100 && d.scrollindex > 0) {
      this.setData({
        scrollindex: d.scrollindex - 1
      })
    } else if (d.endy - d.starty < -100 && d.scrollindex < this.data.totalnum - 1) {
      this.setData({
        scrollindex: d.scrollindex + 1
      })
    }
    this.setData({
      starty: 0,
      endy: 0,
      margintop: 0
    })
  },
  //滑动开始 end


  gotoreadlog(){
    wx.navigateTo({
      url: '../readlog/readlog'
    })
  },


  gotopoetrylog() {
    var plog = this.data.plog;
    if (plog==0){
      wx.showModal({
        title: '失败',
        content: "没有任何诗词阅读记录",
        showCancel: !1,
      });
      return;
    }
    wx.navigateTo({
      url: '../poetryreadlog/poetryreadlog'
    })
  },

  getUserInfo (e) {
    var that = this
    wx.getSetting({
      success (res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              console.log(res)
              wx.hideLoading(res.userInfo);
                 //加入缓存
              wx.setStorageSync('city', res.userInfo.city);
              wx.setStorageSync('province', res.userInfo.province);
              wx.setStorageSync('gender', res.userInfo.gender);
              wx.setStorageSync('nickName', res.userInfo.nickName);
              wx.setStorageSync('avatarUrl', res.userInfo.avatarUrl);
              
              that.setData({
                gender: res.userInfo.gender,
                avatarUrl: res.userInfo.avatarUrl,
              })
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (that.userInfoReadyCallback) {
                that.userInfoReadyCallback(res)
              }
            }
          })

        }
      }
    })
  }

})